function [DVs rectsToTry] = applySVM(im, w)
load_settings;
h = waitbar(0,'Progress');
disp('Generating test rectangles');
upperLefts = [];

for ix = 1:3:size(im,2)
    temp = [repmat(ix, [size(im,1)/3,1]), (1:3:size(im,1))'];
    upperLefts = [upperLefts; temp];
    %     for iy = 1:2:size(im,1)
    %         upperLefts = [upperLefts; ix iy]; % so we get x,y
    perc = ix/(size(im,2));
    waitbar(perc,h,sprintf('Generated %2.1f%% of upperLefts...',perc*100));
    %     end
end

disp('Generating permutations');
rectsToTry = [];
for ind = 1:size(upperLefts,1)
    xxs = upperLefts(ind,1)+binsize:4:size(im,2);
    num_xxs = size(xxs,2);
    xx_ind = 0;
    for xx = xxs %% calc xx
        yys = upperLefts(ind,2)+binsize:4:size(im,1); %% calc all yys for particular xx
        num_ys_left = size(yys,2);
        cur_elem = [repmat(upperLefts(ind), [num_ys_left 1]) repmat(xx, [num_ys_left 1]) yys'];
        rectsToTry = [rectsToTry; cur_elem];
        perc = xx_ind/num_xxs;
        waitbar(perc,h,sprintf('Generated all permutations for %d/%d ...',xx_ind,num_xxs));
        xx_ind = xx_ind+1;
    end

    disp(sprintf('Generated all permutations for %d/%d of upperLefts...',ind,size(upperLefts,1)));
end

disp('Calculating hogs');
hogsToClassify = cellfun(@(x) calc_hog(im, x, reshape_size, binsize), rectsToTry, 'UniformOutput', false);

disp('Applying classifier');
bias = ones(size(hogsToClassify,1),1);
hogs = [hogsToClassify bias];

DVs = w*hogs'; % (1x1551)*(1551xnum_hogs);
DVs = DVs';
end


function [out] = calc_hog(img, rect, reshape_size, binsize)
x = rect(1);
y = rect(2);
xx= x + rect(3);
yy= y + rect(4);
img_rectangle = img(y:yy, x:xx, :);
img_rectangle = imresize(img_rectangle, reshape_size);
hh = vl_hog(single(img_rectangle), binsize);
out = reshape(hh, [size(hh,1)*size(hh,2)*size(hh,3), 1, 1]);
end